package com.tanhua.server.service;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.tanhua.dubbo.server.api.RecommendUserApi;
import com.tanhua.dubbo.server.pojo.RecommendUser;
import com.tanhua.dubbo.server.vo.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author 王加鑫
 * @Date 2021/09/03 19:45
 * @Since JDK 1.8
 *
 *
 * 负责与dubbo服务进行交互
 */
@Service
@Slf4j
public class RecommendUserService {

    @Reference(version = "1.0.0")
    private RecommendUserApi recommendUserApi;

    /**
     * 根据被推荐用户id去mongodb中查询今日佳人数据
     * 在MongoDB中只有被推荐用户的id和用户id和缘分值的存在
     * 所以只能返回被推荐用户id和缘分值
     * @param userId
     * @return
     */
    public RecommendUser queryRecommendUser(Long userId) {
        //rpc调用recommendUserApi去根据userId查询被推荐用户信息
        RecommendUser recommendUser =  this.recommendUserApi.queryWithMaxScore(userId);
        if(null == recommendUser){
            //被推荐用户id无效
            return null;
        }
        return recommendUser;
    }

    /**
     * 根据userId和分页参数查询被推荐用户的集合的数据
     * @param
     * @return
     */
    public PageInfo<RecommendUser> queryRecommendation(Long id, Integer page, Integer pagesize) {
        PageInfo<RecommendUser> pageInfo = recommendUserApi.queryPageInfo(id, page, pagesize);
        if(null == pageInfo){
            return null;
        }
        return pageInfo;
    }


    /**
     * 查询自己与好友的缘分值
     * @param userId
     * @param toUserId
     * @return
     */
    public Double queryScore(Long userId, Long toUserId) {
        Double score = this.recommendUserApi.queryScore(userId, toUserId);
        if(ObjectUtil.isEmpty(score)){
            return score = 90.0;
        }

        return score;
    }

    public List<RecommendUser> queryCardList(Long id, int count) {
        return recommendUserApi.queryCardList(id, count);
    }
}
